﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace WellDone
{
    public partial class EditTimeSensitivePricingDialog : ChildWindow
    {
        public ServiceReferenceOpenRPOS.TimePrice tp;
        
        public EditTimeSensitivePricingDialog()
        {
            InitializeComponent();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (dpStart.Text.Trim().Length>0 && dpEnd.Text.Trim().Length>0)
            {
                if (dpStart.SelectedDate.Value > dpEnd.SelectedDate.Value)
                {
                    MessageBox.Show("start date/time should not be greater than end date/time.");
                }
                else if (dpStart.SelectedDate.Value==dpEnd.SelectedDate.Value)
                {
                     DateTime dstTime = DateTime.Now;
                    DateTime.TryParse(tpStarttime.Value.ToString(), out dstTime);

                    DateTime dEdTime = DateTime.Now;
                    DateTime.TryParse(tpEndtime.Value.ToString(), out dEdTime);

                    TimeSpan tp = dEdTime - dstTime;

                    if (tp.TotalHours==0 && tp.TotalDays==0 && tp.TotalMinutes==0)
                    {
                        MessageBox.Show("start time should not be equal end time.");
                    }
                    else if (tp.TotalMinutes<0 || tp.TotalHours<0 || tp.TotalDays<0)
                    {
                        MessageBox.Show("start time should not be less than end time.");
                    }
                    else
                    {
                        UpdateRecord();
                    }
                }
                else
                {
                    UpdateRecord();
                }
            }
            else
            {
                MessageBox.Show("please select start date/time,end date/time.");
            }
        }

        private void dpStart_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            this.dpStart.Text = this.dpStart.SelectedDate.Value.ToString("M/d/yyyy") + ' ' + DateTime.Now.ToString("hh:mm:ss tt");
        }

        private void dpEnd_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            this.dpEnd.Text = this.dpEnd.SelectedDate.Value.ToString("M/d/yyyy") + ' ' + DateTime.Now.ToString("hh:mm:ss tt");
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }


        public async void UpdateRecord()
        {
            DateTime dtstTime = DateTime.Now;
            DateTime.TryParse(tpStarttime.Value.ToString(), out dtstTime);

            DateTime dtEdTime = DateTime.Now;
            DateTime.TryParse(tpEndtime.Value.ToString(), out dtEdTime);

            DateTime dttpStarttime = new DateTime(dpStart.SelectedDate.Value.Year, dpStart.SelectedDate.Value.Month, dpStart.SelectedDate.Value.Day, dtstTime.Hour, dtstTime.Minute, dtstTime.Second);
            DateTime dtdpEnd = new DateTime(dpEnd.SelectedDate.Value.Year, dpEnd.SelectedDate.Value.Month, dpEnd.SelectedDate.Value.Day, dtEdTime.Hour, dtEdTime.Minute, dtEdTime.Second);

            tp.col_Start = dttpStarttime; tp.bFilterStart = true;
            tp.col_Stop = dtdpEnd; tp.bFilterStop = true;
            tp.col_Interval = Convert.ToByte(cmbIntervals.SelectedIndex); tp.bFilterInterval = true;
            tp.col_Price = Convert.ToDecimal(tbPrice.Text.Trim()); tp.bFilterPrice = true;

            await Globals.db.UpdateTimePriceObject(tp);
            Globals.menuItems.initTimeSensitivePrice(false);
            this.DialogResult = true;
        
        }

        //Accept only decimal Value
        private void tbPrice_KeyDown(object sender, KeyEventArgs e)
        {
            const int KEYCODE_Dot_OnKeyboard = 190;
            const int KEYCODE_Dot_OnNumericKeyPad = 110;

            //Do not allow special characters
            if (Keyboard.Modifiers == ModifierKeys.Shift)
            {
                e.Handled = true;
            }

            //Only allow numbers and decimal
            if ((e.Key >= Key.D0 && e.Key <= Key.D9 ||
                    e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9 ||
                    e.PlatformKeyCode == KEYCODE_Dot_OnKeyboard ||
                    e.PlatformKeyCode == KEYCODE_Dot_OnNumericKeyPad))
            {
                string strkey = e.Key.ToString().Substring(e.Key.ToString().Length - 1,
                        e.Key.ToString().Length - (e.Key.ToString().Length - 1));

                if (e.Key >= Key.D0 && e.Key <= Key.D9 ||
                    e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9)
                {
                    //Do not allow more than 2 digits after decimal point
                    TextBox tb = sender as TextBox;
                    int cursorPosLeft = tb.SelectionStart;
                    int cursorPosRight = tb.SelectionStart + tb.SelectionLength;
                    string result1 = tb.Text.Substring(0, cursorPosLeft) +
                          strkey + tb.Text.Substring(cursorPosRight);
                    string[] parts = result1.Split('.');
                    if (parts.Length > 1)
                    {
                        if (parts[1].Length > 2 || parts.Length > 2)
                        {
                            e.Handled = true;
                        }
                    }
                }

                //Do not allow multiple decimal points
                if (((TextBox)sender).Text.Contains(".") && (e.PlatformKeyCode ==
                      KEYCODE_Dot_OnKeyboard ||
                      e.PlatformKeyCode == KEYCODE_Dot_OnNumericKeyPad))
                {
                    e.Handled = true;
                }
            }
            else
            {
                e.Handled = true;
            }

            //Do not allow alphabets and space
            if (e.Key >= Key.A && e.Key <= Key.Z || e.Key == Key.Space)
            {
                e.Handled = true;
            }
        }
    }
}

